package com.bestcem.xm.award.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.survey.grpc.v1.services.BpSurveyServiceGrpc;
import com.bestcem.xm.survey.grpc.v1.services.GetSurveyScoreBySeqRequest;
import com.bestcem.xm.survey.grpc.v1.services.GetSurveyScoreBySeqResponse;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
import zhongyan.xm.base.v1.Base;

/**
 * 答卷 grpc客户端
 *
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/23 13:33
 */
@Slf4j
@Service
public class AwardSurveyGrpcService {

    @GrpcClient(GrpcClientNames.DS_SURVEY)
    private BpSurveyServiceGrpc.BpSurveyServiceBlockingStub stub;

    /**
     * 获取问卷评分
     *
     * @param request
     * @return
     */
    public GetSurveyScoreBySeqResponse getSurveyScoreBySeq(GetSurveyScoreBySeqRequest request) {
        try {
            return stub.getSurveyScoreBySeq(request);
        } catch (Exception e) {
            log.error("[award] survey getSurveyScoreBySeq {} Error; ErrorMsg: {};", request, e.getMessage(), e);
            return GetSurveyScoreBySeqResponse.newBuilder().setStatus(error("survey服务调用失败")).build();
        }
    }

    /**
     * 构造错误状态
     *
     * @param message 错误信息
     * @return
     */
    private Base.Status error(String message) {
        Base.Status.Builder status = Base.Status.newBuilder();
        status.setMessage(message);
        status.setCode(Base.Code.ERROR);
        return status.build();
    }
}
